package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.LabelListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class LabelListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();

    public LabelListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LabelListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.label_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LabelListItemBinding root = viewHolder.getBinding();
        JSONObject item = items.getJSONObject(position);
        String label = item.getString("NAME");
        if(StringUtil.isNull(label))
            label = item.getString("Name");
        if(StringUtil.isNull(label))
            label = item.getString("REQUIREMENT");
        root.label.setText(label);
        if (item.getBooleanValue("selected"))
            root.selected.setVisibility(View.VISIBLE);
        else
            root.selected.setVisibility(View.GONE);

        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void remove(int position){
        items.remove(position);
        notifyDataSetChanged();
    }

    public JSONArray getItems(){
        return items;
    }

    public String getSelectItemId(){
        for (int i = 0, j = items.size(); i < j; i++) {
            JSONObject item = (JSONObject) items.get(i);
            if (item.getBooleanValue("selected")) {
                return StringUtil.dTs(item.getString("ID"));
            }
        }
        return null;
    }

    public String getSelectItemName(){
        for (int i = 0, j = items.size(); i < j; i++) {
            JSONObject item = (JSONObject) items.get(i);
            if (item.getBooleanValue("selected")) {
                String label = item.getString("NAME");
                if(StringUtil.isNull(label))
                    label = item.getString("Name");
                if(StringUtil.isNull(label))
                    label = item.getString("REQUIREMENT");
                return label;
            }
        }
        return null;
    }

    public JSONObject getSelectItem(){
        for (int i = 0, j = items.size(); i < j; i++) {
            JSONObject item = (JSONObject) items.get(i);
            if (item.getBooleanValue("selected")) {
                return item;
            }
        }
        return null;
    }

    private class ViewHolder {
        private LabelListItemBinding binding;

        public ViewHolder(LabelListItemBinding binding) {
            this.binding = binding;
        }

        public LabelListItemBinding getBinding() {
            return binding;
        }
    }
}
